﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCP.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCP.UI.Blocks;
using Baosight.ZNH.ZMCF.UI.Download;

namespace Baosight.ZNH.ZMCP.UI
{
    public partial class ZMCP07 : EFViewCmes
    {
        ProfessionalPlanBlock block = new ProfessionalPlanBlock();
        public ZMCP07()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            initData.AddBlock(block.getPlanMonth("cmes.cp.planMonth", true));
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "其它专业计划-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            List<EIRow> EFGridCheckedRows = this.grid.CheckedEIRows;
            int count = EFGridCheckedRows.Count;
            if (count <= 0)
            {
                BaseTools.OpenEFWindowAlert("请选择一条记录进行修改！", WindowAlertClosed);
                return;
            }
            else if (count > 1)
            {
                BaseTools.OpenEFWindowAlert("最多只能选择一条记录进行修改！", WindowAlertClosed);
                return;
            }
            else
            {
                EIRow row = this.grid.CheckedEIRows[0];
                EIInfo info = new EIInfo();
                info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCP07");
                info.Attributes.Add(EIConstants.MethodNameKey, "queryFiles");
                info.Attributes.Add("fdKeyId", row["fdKeyId"]);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(info, OnDoWorkCompleted);
            }

        }
        /// <summary>
        /// 修改按钮中的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            eiInfo.AddBlock(e.EIInfo.GetBlock("filesresult"));

            base.OnUpdateButtonClickPopup(eiInfo, "其它专业计划-修改", "update", null, TempButton, null);
        }

        /// <summary>
        /// 点击上报事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnReportButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("report", "上报", sender, e);
        }

        /// <summary>
        /// 在删除和上报时进行校验，已上报的不能删除和上报
        /// </summary>
        /// <param name="_eIRows"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            foreach (EIRow row in _eIRows)
            {
                if (!row["fdPlanStatus"].Equals("草拟"))
                {
                    BaseTools.OpenEFWindowAlert("只有草拟的记录可以" + TempButton.Content + "，请重新选择!", null);
                    this.TempButtonOpen(true);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(block.getPlanMonth("cmes.cp.planMonth", false));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.otherPlanType"));
            ProfessionalPlanQT view = new ProfessionalPlanQT(_dialogType);//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(_eIInfo);
            if (_dialogType.Equals("insert"))
            {
                view.fdPlanYear.Value = DateTime.Now.Year.ToString();
                view.fdPlanMonth.Value = DateTime.Now.Month.ToString();
                view.fdPlanStatus.Value = "草拟";
            }
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "其它专业计划-查看", "detail", null, sender, e);
        }

        #region 下载附件
        /// <summary>
        /// 下载附件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Download_HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            TempHyperlink = sender as HyperlinkButton;
            TempHyperlink.IsEnabled = false;
            EIRow row = (TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo info = new EIInfo();
            info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCP07");
            info.Attributes.Add(EIConstants.MethodNameKey, "queryFiles");
            info.Attributes.Add("fdKeyId", row["fdKeyId"]);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(info, OnQueryDownloadFilesCompleted);
        }

        /// <summary>
        /// 下载附件按钮中查询附件的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnQueryDownloadFilesCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            DownloadView view = new DownloadView();
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(e.EIInfo);

            CmesWindows _win = new CmesWindows();//构造一个弹出窗口
            _win.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _win.Header = view.eTitle;//设置Title
            _win.Content = view;
            _win.Closed += delegate { if (TempHyperlink != null) { TempHyperlink.IsEnabled = true; TempHyperlink = null; } };
            _win.ShowDialog();//模态显示弹出窗口
        }
        #endregion
    }
}

